package wstrader.task.reference;
import java.sql.*;
import pc.system.*;
import wstrader.task.*;

public class MasterList extends Role
{
  private String table_name = null;
  private String table_field = null;
  private Record ref = null;

  public MasterList(Tasks owner)
  {
    super(owner,"master-list");
  }

  public String getMasterListId()
  {
    String value = getRequest().getValue("master_list");
    if (value == null) value = table_name;
    return value;
  }

  public String getMasterTable()
  {
    return table_name;
  }

  public Record getMasterRef()
  {
    return ref;
  }

  public ResultSet getMasterList()
  {
    if (table_name.equals("grade_master")) {
      String sql =
          "SELECT * FROM grade_master " +
          "WHERE origin_id  = " + getRequest().getIntValue("origin_id",1) + " " +
          "  AND quality_id = " + getRequest().getIntValue("quality_id",2);
      return getSQLCommand().executeQuery(sql);
    }
    return getMasterList(table_name,table_field);
  }

  public long doTask()
  {
    String s = getRequest().getValueString("master_list").toLowerCase().trim();
    table_name = s;
    table_field = "name";
    int p = s.indexOf(';');
    if (p > 0) {
      table_name  = s.substring(0,p);
      table_field = s.substring(p+1);
    }
    if (table_name.length() == 0) table_name = "company_master";

    int task_id     = getRequest().getIntValue("task_id");
    long focused_id = getRequest().getLongValue("master_list_value");


    switch (task_id) {
      case 1 : // Update
        focused_id = doUpdate(table_name,focused_id);
        break;
      case 2 : // Delete
        focused_id = doDelete(table_name,focused_id);
        focused_id = 1;
        break;
      default : //View only
        break;
    }
    this.setFocusedID(focused_id);
    ref = getRecord(table_name,focused_id);
    return focused_id;
  }

}


